package kontrol;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import database.DALException;

public class Validator {

	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	public String[] validateHELTAL(String input, String type, int min, int max) {
		String error = null;
		int heltal = 1;
		try {
			heltal = Integer.parseInt(input);
		} catch (Exception e) {
			error = "the entered value must be of type integer";
			input = null;
		}
		if (heltal > max || heltal < min) {
			error = "the number must be between " + min + " - " + max;
			input = null;
		}
		String[] result = new String[] { type, input, error };
		return result;
	}

	public String[] validateNAVN(String input, String type) {
		String error = null;
		if (input.length() > 20 || input.length() < 2) {
			error = "the name must be between 2 - 20 characters long";
			input = null;
		}
		String[] result = new String[] { type, input, error };
		return result;
	}

	public String[] validateTOLERANCE(String input, String type, String netto) {
		String error = null;
		double tolerance = 0;
		try {
			tolerance = Double.parseDouble(input);
		} catch (Exception e) {
			error = "the entered value must be of type decimal/double";
			input = null;
		}
		if (tolerance > 10.0 || tolerance < 0.1) {
			if (error == null) {
				error = "the tolerance must be min. 0.1 % and max. 10 %";
				input = null;
			} else {
				error = error
						+ "; the tolerance must be min. 0.1 % and max. 10 %";
				input = null;
			}
		}

		if (netto != null) {
			int weight = Integer.parseInt(netto);

			if ((weight * tolerance) / 100 < 1) {
				input = null;
				if (error == null) {
					error = "the tolerance must have a min. of 1 gram";
				} else
					error = error + " the tolerance must have a min. of 1 gram";
			}
		}
		String[] result = new String[] { type, input, error };
		return result;
	}

	public String[] validateDOUBLE(String input, String type) {
		String error = null;
		double temp = 1;
		try {
			temp = Double.valueOf(input);
		} catch (NumberFormatException e) {
			error = "the entered value must be of type decimal/double";
			input = null;
		}
		String[] result = new String[] { type, input, error };
		return result;
	}

	public String[] validateWEIGHT(String input, String type) {
		String error = null;
		int weight = 50;
		try {
			weight = Integer.parseInt(input);
		} catch (Exception e) {
			error = "the entered value must be of type integer";
			input = null;
		}
		if (weight > 6000 || weight < 50) {
			if (error == null) {
				error = "the Netto weight must be between 50 g. - 6000 g.";
			}
			input = null;
		}
		String[] result = new String[] { type, input, error };
		return result;
	}

	public String[] validateDATE(String input, String type) {
		String error = null;
		try {
			df.parse(input);
			String[] temp = input.split("-", 3);
			int year = Integer.parseInt(temp[0]);
			int month = Integer.parseInt(temp[1]);
			int date = Integer.parseInt(temp[2]);

			if (year < 2013) {
				error = "the entered year must be 2013 or larger";
				input = null;
			}

			if (month < 0 || month > 12) {
				if (error == null) {
					error = "the entered month must be between 01 - 12";
				} else {
					error = error + "the entered month must be between 01 - 12";
				}
				input = null;
			}

			if (date < 0 || date > 31) {
				if (error == null) {
					error = "the entered date must be between 01 - 31";
				} else {
					error = error + "the entered date must be between 01 - 31";
				}
				input = null;
			}

		} catch (ParseException e) {
			error = "the entered value does not match the date format";
			input = null;
		}
		String[] result = new String[] { type, input, error };
		return result;
	}

	public String[] validateCPR(String input, String type) {

		String error = null;
		String[] result;

		if (input.length() == 10) {

			int dateValue, monthValue, yearValue, cprValue;

			dateValue = 0;
			monthValue = 0;
			yearValue = 0;
			cprValue = 0;

			try {
				String date = input.substring(0, 2);
				dateValue = Integer.parseInt(date);

				String month = input.substring(2, 4);
				monthValue = Integer.parseInt(month);

				String year = input.substring(4, 6);
				yearValue = Integer.parseInt(year);

				String cpr = input.substring(6, 10);
				cprValue = Integer.parseInt(cpr);

			} catch (NumberFormatException e) {
				e.printStackTrace();
				error = "the entered value does not match the cpr format";
				input = null;
				result = new String[] { type, input, error };
				return result;
			}

			if (dateValue < 0 || dateValue > 31) {
				error = "the entered date is not right";
				input = null;
			}

			if (monthValue < 0 || monthValue > 12) {
				if (error == null) {
					error = "the entered month must be between 01 - 12";
				} else {
					error = error + "the entered month must be between 01 - 12";
				}
				input = null;
			}
		}

		else {
			error = "the entered cpr does not have the right length";
			input = null;
			result = new String[] { type, input, error };
			return result;
		}

		result = new String[] { type, input, error };
		return result;
	}

	// Password validering

	public String[] validatePASSWORD(String input, String type)
			throws DALException {
		String error = null;
		char[] atozArray = new char[26];
		char[] AtoZArray = new char[26];
		char[] numbersArray = new char[10];
		int az = 0;
		int AZ = 0;
		int num = 0;

		// populating arrays
		for (int i = 0; i < atozArray.length; i++) {
			atozArray[i] = (char) (i + 'a');
		}

		for (int i = 0; i < AtoZArray.length; i++) {
			AtoZArray[i] = (char) (i + 'A');
		}

		for (int i = 0; i < numbersArray.length; i++) {
			numbersArray[i] = (char) (i + '0');
		}

		for (int i = 0; i < input.length(); i++) {
			if (Arrays.binarySearch(atozArray, input.charAt(i)) > 0)
				az++;

			if (Arrays.binarySearch(AtoZArray, input.charAt(i)) > 0)
				AZ++;

			if (Arrays.binarySearch(numbersArray, input.charAt(i)) > 0)
				num++;
		}

		if (num < 0 && az < 0 && AZ < 0) {
			error = "Password does not meet requirements: " + input
					+ " is not strong enough!";
			input = null;
		}

		String[] result = new String[] { type, input, error };
		return result;
	}

	public String generatePASSWORD() {

		char[] password = new char[8];
		char[] atozArray = new char[26];
		char[] AtoZArray = new char[26];
		char[] numbersArray = new char[10];
		char[] specialArray = { '.', '-', '_', '+', '!', '?', '=' };

		// populating arrays
		for (int i = 0; i < atozArray.length; i++) {
			atozArray[i] = (char) (i + 'a');
		}

		for (int i = 0; i < AtoZArray.length; i++) {
			AtoZArray[i] = (char) (i + 'A');
		}

		for (int i = 0; i < numbersArray.length; i++) {
			numbersArray[i] = (char) (i + '0');
		}

		// getting the required content for array, one of each type.
		password[0] += (atozArray[(int) (Math.random() * atozArray.length)]);
		password[1] += (AtoZArray[(int) (Math.random() * AtoZArray.length)]);
		password[2] += (numbersArray[(int) (Math.random() * numbersArray.length)]);
		password[3] += (specialArray[(int) (Math.random() * specialArray.length)]);

		// fillilng out the rest of the passwordlenght (8) with random chars
		// from random array
		for (int i = 4; i < 8; i++) {
			int temp = (int) Math.random() * 4;

			switch (temp) {
			case 0:
				password[i] += (atozArray[(int) (Math.random() * atozArray.length)]);
				break;
			case 1:
				password[i] += (AtoZArray[(int) (Math.random() * AtoZArray.length)]);
				break;
			case 2:
				password[i] += (numbersArray[(int) (Math.random() * numbersArray.length)]);
				break;
			case 3:
				password[i] += (specialArray[(int) (Math.random() * specialArray.length)]);
				break;
			}
		}

		// randomizing the array
		for (int i = 0; i < password.length; i++) {
			int random = (int) (Math.random() * password.length);
			char temp = password[random];
			password[random] = password[i];
			password[i] = temp;
		}
		return new String(password);
	}

}
